package web.base.system.service;

import blue.core.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import web.base.system.dao.RoleDao;
import web.base.system.model.Role;
import web.core.util.ErrorCode;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-02
 */
@Service
@Transactional
public class RoleService
{
	private static final String CODE = "code";

	private RoleDao roleDao;

	public RoleService()
	{
	}

	public Role save(Role role)
	{
		if (roleDao.exist(role, CODE))
			throw ErrorCode.EXISTS.newException(CODE);

		if (role.getId() == null)
		{
			roleDao.save(role);
		}
		else
		{
			roleDao.update(role);
		}
		return roleDao.get(role.getId());
	}

	public Role get(Integer id)
	{
		return roleDao.get(id);
	}

	public void delete(Integer id)
	{
		roleDao.deleteId(id);
	}

	public Page list(Role param, Page page)
	{
		return roleDao.listPage(param, page);
	}

	@Autowired
	public void setRoleDao(RoleDao roleDao)
	{
		this.roleDao = roleDao;
	}
}
